package com.android.tea.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.tea.R;
import com.android.tea.entity.Favorites;

public class FavoritesAdapter extends BaseAdapter {

	private LayoutInflater inflater;
	private List<Favorites> list;
	private Context context;

	public FavoritesAdapter(Context context, List<Favorites> list) {
		super();
		this.context = context;
		this.list = list;
		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Favorites getItem(int position) {
		if (getCount() >= position + 1) {
			return list.get(position);
		} else {
			return null;
		}
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final Favorites order = list.get(position);
		if (order == null) {
			return null;
		}
		final ViewHolder holder;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.favorites_item, null);
			holder = new ViewHolder();
			holder.item = (RelativeLayout) convertView
					.findViewById(R.id.relativeLayout1);
			holder.favorite_image = (ImageView) convertView
					.findViewById(R.id.favorite_image);
			holder.favorite_prices = (TextView) convertView
					.findViewById(R.id.favorite_prices);
			holder.favorite_info = (TextView) convertView
					.findViewById(R.id.favorite_info);

			holder.id = position;
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		OnClickListener clickListener = new OnClickListener() {
			@Override
			public void onClick(View v) {
				switch (v.getId()) {
				case R.id.relativeLayout1:

					break;
				default:
					break;
				}
			}

		};

		holder.item.setOnClickListener(clickListener);

		setData(order, holder);
		return convertView;
	}

	static class ViewHolder {
		int id;
		RelativeLayout item;
		ImageView favorite_image;
		TextView favorite_prices;
		TextView favorite_info;
	}

	private void setData(Favorites info, ViewHolder holder) {
		String ordernumber = info.getFav_info();
		String price = info.getFav_price();
		holder.favorite_info.setText(ordernumber);
		holder.favorite_prices.setText(price);

	}

}
